<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Person name expander
 * 
 * Expands person names (foaf:name) to include variant notation forms.
 * 
 * Pan, Peter -> Peter Pan
 * Pan, Peter -> P. Pan
 * Peter Pan -> P. Pan
 */
class entityresolution_datafilter_PersonNameExpander implements entityresolution_DataFilter
{
	/**
	 * RDF schema document.
	 * @var simplerdf_RdfSchemaDocument
	 */
	protected $rdfSchemaDocument;
	
	/**
	 * Person RDF types.
	 * @var array[int]string
	 */
	protected $personRdfTypes;
	
	/**
	 * Create a new person name expander instance.
	 * @param simplerdf_RdfSchemaDocument $rdfSchemaDocument RDF schema document
	 */
	public function __construct($rdfSchemaDocument)
	{
		$this->rdfSchemaDocument = $rdfSchemaDocument;
		
		$personType = 'http://xmlns.com/foaf/0.1/Person';
		$personSubTypes = $rdfSchemaDocument->getSubClassUriList($personType);
		
		$this->personRdfTypes = $personSubTypes;
		$this->personRdfTypes[] = $personType;
	}
	
	/**
	 * Filter the data of an entity.
	 * @param entityresolution_Entity $entity
	 */
	public function filterData($entity)
	{
		// get the entity's RDF types 
		$rdfResource = $entity->getSourceData()->getResourceBySubject($entity->getSubjectUri());
		$rdfTypes = $rdfResource->getRdfTypes();

		// expand the entity's RDF types with sub types
		$expandedRdfTypes = $rdfTypes;
		foreach($rdfTypes as $rdfType)
		{
			$subClassUriList = $this->rdfSchemaDocument->getSubClassUriList($rdfType);
			$expandedRdfTypes = array_merge($expandedRdfTypes, $subClassUriList);
		}
		$expandedRdfTypes = array_unique($expandedRdfTypes);
		
		// check if any of the entity's RDF types match the list of person RDF types
		$isPersonType = false;
		foreach($expandedRdfTypes as $rdfType)
		{
			if(in_array($rdfType, $this->personRdfTypes))
			{
				$isPersonType = true;
				break;
			}
		}
		
		if($isPersonType)
		{
			$properties = $entity->getProperties();
			foreach($properties as $property)
			{
				/* @var $property entityresolution_Property */
				
				$predicate = $property->getPredicate();
				
				if($predicate === 'http://xmlns.com/foaf/0.1/name')
				{
					$values = $property->getValues();
					foreach($values as $value)
					{
						/* @var $value entityresolution_Value */
						
						$name = $value->getValue();
						$valueForms = $value->getValueForms();
						
						if(preg_match('/^([^,. ]+) ([^,.]+)$/u', $name, $nameParts))
						{
							// Peter Pan -> P. Pan
							$givenName = trim($nameParts[1]);
							$familyName = trim($nameParts[2]);
							
							$givenNameParts = preg_split('/[ -]/', $givenName);
							$initials = '';
							foreach($givenNameParts as $givenNamePart)
							{
								$initials .= substr($givenNamePart, 0, 1) . '.';
							}
							$valueForms[] = $initials . ' ' . $familyName;
						}
						else if(preg_match('/^([^,]+?)( [\p{Ll} ]+)?,([^,]+)$/u', $name, $nameParts))
						{
							// Heydt von der, Julian August -> Julian August von der Heydt (lower case name parts are assumed to be part of the 'middle name') 
							$givenName = trim($nameParts[3]);
							$middleName = trim($nameParts[2]);
							$familyName = trim($nameParts[1]);
							
							$nameReordered = $givenName . ' ' . $middleName . ' ' . $familyName;
							$nameReordered = trim(preg_replace('/ +/', ' ', $nameReordered));
							$valueForms[] = trim($nameReordered);
							
							if(preg_match('/[^ -.]{2,}/', $nameParts[3]))
							{
								// Heydt von der, Julian August -> J.A. von der Heydt
								// Zandvliet, Harold J.W. -> H.J.W. Zandvliet
								$givenNameParts = preg_split('/[ -.]/', $givenName);
								$initials = '';
								foreach($givenNameParts as $givenNamePart)
								{
									if(trim($givenNamePart) !== '')
									{
										$initials .= substr($givenNamePart, 0, 1) . '.';
									}
								}
								
								$nameReordered = $initials . ' ' . $middleName . ' ' . $familyName;
								$nameReordered = trim(preg_replace('/ +/', ' ', $nameReordered));
								$valueForms[] = trim($nameReordered);
							}
						}	
						
						$value->setValueForms($valueForms);
						
//						print_r($valueForms);
					}
				}
			}
			$entity->setProperties($properties);
		}
	}
}